﻿using System;
using System.Collections.Generic;
using System.Reflection;
using Cms.Application.Admin.Views;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;
using Cms.Domain.Wrappers;

namespace Cms.Application.Admin.Presenters
{
    public class FrediumPluginsPopupPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly IFrediumPluginsPopupView _view;

        public FrediumPluginsPopupPresenter(IFrediumPluginsPopupView view)
            : this(view, new RepositoryFactory())
        {
        }
        
        public FrediumPluginsPopupPresenter(IFrediumPluginsPopupView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            if (!_administrator.HasPermission.EditContent)
                _view.RedirectToPermissionDeniedPage();
        }
        
        public void Initialize()
        {
            PluginHandler.SyncPlugins(SystemRepository);
            IList<PluginDescription> pluginDescriptions = new List<PluginDescription>();
            IList<Plugin> plugins = SystemRepository.GetPlugins();
            foreach (Plugin p in plugins)
            {
                string translationFile = "Admin/Translations/Plugins/" + p.GetClassName() + ".xml";
                string name = Translator.GetText(translationFile, "PluginName", _view.Localization);
                string description = Translator.GetText(translationFile, "PluginDescription", _view.Localization);
                pluginDescriptions.Add(new PluginDescription(name, description, p.GetType() == Type.GetType("BaseTemplatedPlugin"), p));
            }
            _view.SetPlugins(pluginDescriptions);
        }

        public void GetPlugin()
        {
            if (_view.SelectedPluginID < 0)
                return;
            Plugin plugin = SystemRepository.GetPlugin(_view.SelectedPluginID);
            string description = Translator.GetText("Admin/Translations/Plugins/" + plugin.GetClassName() + ".xml",
                                                    "PluginDescription",
                                                    _view.Localization);
            _view.SetPluginInfo(description, plugin.FullClassName, plugin.AssemblyName);
            if (plugin is TemplatablePlugin)
                _view.SetTemplates((plugin as TemplatablePlugin).GetTemplates(_administrator.CurrentSite.ID), true);
            else
                _view.SetTemplates(new List<PluginTemplate>(), false);
            IList<PluginPropertyDescription> properties = new List<PluginPropertyDescription>();
            foreach (PropertyInfo pi in PluginHandler.GetPluginProperties(_view.SelectedPluginID, SystemRepository))
            {
                var property = new PluginPropertyDescription();
                property.Name = pi.Name;
                property.FullClassName = plugin.FullClassName;
                property.ClassName = plugin.GetClassName();
                property.Info = pi;
                properties.Add(property);
            }
            _view.SetProperties(properties);
        }

        public IList<Category> GetRootCategories()
        {
            return _administrator.CurrentSite.GetRootCategories();
        }
        
        public IDictionary<string, string> GetEnumerationTypeItems(string enumeration, string className)
        {
            IDictionary<string, string> items = new Dictionary<string, string>();
            switch (enumeration)
            {
                case "SortDirections":
                    items.Add("Ascending",
                              Translator.GetText("Admin/Translations/Plugins/" + className + ".xml", "Ascending",
                                                 _view.Localization));
                    items.Add("Descending",
                              Translator.GetText("Admin/Translations/Plugins/" + className + ".xml", "Descending",
                                                 _view.Localization));
                    break;
                case "ContentSortMode":
                    items.Add("PublishedTimestamp",
                              Translator.GetText("Admin/Translations/Plugins/" + className + ".xml",
                                                 "PublishedTimestamp", _view.Localization));
                    items.Add("SortKey",
                              Translator.GetText("Admin/Translations/Plugins/" + className + ".xml", "SortKey",
                                                 _view.Localization));
                    break;
                case "MediaSortMode":
                    items.Add("UploadedTimestamp",
                              Translator.GetText("Admin/Translations/Plugins/" + className + ".xml", "UploadedTimestamp",
                                                 _view.Localization));
                    items.Add("SortKey",
                              Translator.GetText("Admin/Translations/Plugins/" + className + ".xml", "SortKey",
                                                 _view.Localization));
                    break;
                case "LocaleTranslationOption":
                    items.Add("CurrentLocale",
                              Translator.GetText("Admin/Translations/Plugins/" + className + ".xml", "CurrentLocale",
                                                 _view.Localization));
                    items.Add("NativeLocale",
                              Translator.GetText("Admin/Translations/Plugins/" + className + ".xml", "NativeLocale",
                                                 _view.Localization));
                    break;
                case "ListContentReadMoreModes":
                    items.Add("Bookmark",
                              Translator.GetText("Admin/Translations/Plugins/" + className + ".xml", "Bookmark",
                                                 _view.Localization));
                    items.Add("Parameter",
                              Translator.GetText("Admin/Translations/Plugins/" + className + ".xml", "Parameter",
                                                 _view.Localization));
                    break;
                case "ReadMoreVisibilityMode":
                    items.Add("AlwaysVisible",
                              Translator.GetText("Admin/Translations/Plugins/" + className + ".xml", "AlwaysVisible",
                                                 _view.Localization));
                    items.Add("OnlyVisibleWhenContentIsTruncated",
                              Translator.GetText("Admin/Translations/Plugins/" + className + ".xml", "OnlyVisibleWhenContentIsTruncated",
                                                 _view.Localization));
                    break;
            }
            return items;
        }
    }
}